package com.rul.mars.user.controller;

import com.rul.mars.common.api.CommonPage;
import com.rul.mars.common.api.CommonResult;
import com.rul.mars.mbg.domain.ResourceParam;
import com.rul.mars.mbg.model.Resource;
import com.rul.mars.user.service.ResourceService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * @author RuL
 */
@Api(tags = "ResourceController", description = "资源服务接口")
@RestController
@RequestMapping(path = "/resource")
public class ResourceController {

    @Autowired
    private ResourceService resourceService;

    /**
     * 添加资源
     *
     * @param param 资源参数
     * @return 添加结果
     */
    @ApiOperation("添加资源")
    @PostMapping(value = "/create")
    public CommonResult create(@RequestBody ResourceParam param) {
        int count = resourceService.create(param);
        if (count > 0) {
            return CommonResult.success(count);
        } else {
            return CommonResult.failed();
        }
    }

    /**
     * 修改资源
     *
     * @param resource 资源信息
     * @return 修改结果
     */
    @ApiOperation("修改资源")
    @PostMapping(value = "/update")
    public CommonResult update(@RequestBody Resource resource) {
        int count = resourceService.update(resource);
        if (count > 0) {
            return CommonResult.success(count);
        } else {
            return CommonResult.failed();
        }
    }

    /**
     * 根据ID获取资源详情
     *
     * @param id 资源id
     * @return 资源详情
     */
    @ApiOperation("根据ID获取资源详情")
    @GetMapping(value = "/{id}")
    public CommonResult<Resource> getItem(@PathVariable Long id) {
        Resource umsResource = resourceService.getItem(id);
        return CommonResult.success(umsResource);
    }

    /**
     * 根据ID删除后台资源
     *
     * @param id 资源ID
     * @return 删除结果
     */
    @ApiOperation("根据ID删除后台资源")
    @PostMapping(value = "/delete/{id}")
    public CommonResult delete(@PathVariable Long id) {
        int count = resourceService.delete(id);
        if (count > 0) {
            return CommonResult.success(count);
        } else {
            return CommonResult.failed();
        }
    }

    /**
     * 分页查询资源信息
     *
     * @param nameKeyword 资源名关键字
     * @param urlKeyword  资源url关键字
     * @param pageSize    页面大小
     * @param pageNum     页面号
     * @return 资源分页信息
     */
    @ApiOperation("分页模糊查询资源")
    @GetMapping(value = "/list")
    public CommonResult<CommonPage<Resource>> list(@RequestParam(required = false) String nameKeyword,
                                                   @RequestParam(required = false) String urlKeyword,
                                                   @RequestParam(value = "pageSize", defaultValue = "5") Integer pageSize,
                                                   @RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum) {
        List<Resource> resourceList = resourceService.list(nameKeyword, urlKeyword, pageSize, pageNum);
        return CommonResult.success(CommonPage.restPage(resourceList));
    }

    /**
     * 查询所有资源
     *
     * @return 所有资源信息
     */
    @ApiOperation("查询所有资源")
    @GetMapping(value = "/listAll")
    public CommonResult<List<Resource>> listAll() {
        List<Resource> resourceList = resourceService.listAll();
        return CommonResult.success(resourceList);
    }

    /**
     * 初始化资源角色关联数据
     *
     * @return 操作结果
     */
    @ApiOperation("初始化资源角色关联数据")
    @GetMapping(value = "/initResourceRolesMap")
    public CommonResult initResourceRolesMap() {
        Map<String, List<String>> resourceRolesMap = resourceService.initResourceRolesMap();
        return CommonResult.success(resourceRolesMap);
    }
}
